﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.Forms.InOutMoneys
{
    public partial class AddInOutMoney : Form
    {
        //event edit
        public delegate void ButtonSaveEventHandler();
        public event ButtonSaveEventHandler BtnAdd;

        List<InOutMoneyType> _dataSourseType = new List<InOutMoneyType>();

        public int? ItemCategoryId { get; set; }

        public AddInOutMoney()
        {
            InitializeComponent();
        }

        //esc to exit
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void AddInOutMoney_Load(object sender, EventArgs e)
        {
            btnSave.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); // trong suot
            using (var db = new DatabaseEntities())
            {
                //khoi tao giu lieu type giao dich
                _dataSourseType = db.InOutMoneyTypes.ToList();
            }
            foreach (Button control in Controls.OfType<Button>())
            {
                if (control.Name != "btnClose")
                {
                    control.MouseHover += btn_MouseHover;
                    control.MouseLeave += btn_MouseLeave;
                }
            }
        }


        private void btn_MouseHover(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton_hover;
            }
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.woodbutton;
            }
        }

        private void cbbItemGroupType_SelectedValueChanged(object sender, EventArgs e)
        {
            var model = _dataSourseType.Where(x => x.Group == cbbItemGroupType.Text.Trim()).OrderBy(x => x.Name).ToList();
            cbbType.DataSource = model;
            cbbType.DisplayMember = "Name";
            cbbType.ValueMember = "Name";
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            lblPrice.Text = txtPrice.formatText();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validatorForm.Validate())
            {
                if (BtnAdd != null)
                {
                    try
                    {
                        var type = cbbType.Text;
                        var groupForSearch = cbbItemGroupType.Text; //phan loai group cho thi chi de tien tim kiem
                        var inOutMoneyInsert = new InOutMoney()
                        {
                            Datetime = DateTime.Now,
                            ByName = "User dang login",
                            Type = type,
                            Description = txtDescription.Text,
                            Money = decimal.Parse(txtPrice.Text),
                            Group = groupForSearch,
                        };
                        if (InOutMoneyDa.Insert(inOutMoneyInsert) > 0)
                        {
                            BtnAdd();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

    }
}
